package com.jwthhealth_pub.old.report.presenter;

import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.HraDataRes;
import com.jwthhealth_pub.old.report.model.resbody.HraDateRes;
import com.jwthhealth_pub.old.report.view.fragment.ReportContentFragment;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

public class ReportPresenter implements ReportContect.presenter {

    private static final String TAG = makeLogTag(ReportPresenter.class);
    private ReportContect.view mView;
    private List<HraDateRes.DataBean> mUserDateList;
    private HraDateRes.DataBean mDate;
    private HraDataRes.DataBean mData;
    private final ReportContentFragment mFragment;

    public HraDataRes.DataBean getmData() {
        return mData;
    }

    public HraDateRes.DataBean getmDate() {
        return mDate;
    }

    public void setmDate(HraDateRes.DataBean mDate) {
        this.mDate = mDate;
    }

    public List<HraDateRes.DataBean> getmUserDateList() {
        return mUserDateList;
    }

    public void setmUserDateList(List<HraDateRes.DataBean> mUserDateList) {
        this.mUserDateList = mUserDateList;
    }

    public ReportPresenter(ReportContect.view mView) {
        this.mView = mView;
        mView.setPresenter(this);
        mFragment = (ReportContentFragment) mView;
    }

    @Override
    public void loadDate(final HraDateRes.DataBean data) {
        final String id = data.getNid();
        setmDate(data);

        final LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        final String uid = userinfoModel.getId();

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                String md5 = getMD5(uid + ApiHelper.macAddress + id + ApiHelper.deviceId + userinfoModel.getAndroidtoken());
//                ApiHelper.request(ApiHelper.BASE_URL + "hra-new/detail?dev=" + ApiHelper.deviceId + "&mac=" + ApiHelper.macAddress +
//                        "&uid=" + uid + "&id=" + id + "&sign="+md5);
//            }
//        }.start();

        ObservableOnSubscribe<HraDataRes> source = new ObservableOnSubscribe<HraDataRes>() {

//            private RequestService requestService;

            @Override
            public void subscribe(@NonNull ObservableEmitter<HraDataRes> e) throws Exception {
                //用户数据
                final LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                final String uid = userinfoModel.getId();

                Call<HraDataRes> call = ApiHelper.getNewHraData(uid, id, userinfoModel.getAndroidtoken());
                final ObservableEmitter<HraDataRes> _e = e;
                call.enqueue(new Callback<HraDataRes>() {
                    @Override
                    public void onResponse(@android.support.annotation.NonNull Call<HraDataRes> call, @android.support.annotation.NonNull Response<HraDataRes> response) {
                        _e.onNext(response.body());
                    }

                    @Override
                    public void onFailure(@android.support.annotation.NonNull Call<HraDataRes> call, @android.support.annotation.NonNull Throwable t) {
                        _e.onError(new Exception(t.toString()));
                        LogUtil.e(t.toString(), TAG);
                    }
                });
            }
        };

        Observable<HraDataRes> observable = Observable.create(source);
        observable.map(new Function<HraDataRes, HraDataRes.DataBean>() {
            @Override
            public HraDataRes.DataBean apply(@NonNull HraDataRes HraDataRes) throws Exception {
                String code = HraDataRes.getCode();
                if (code.equals("0")) {
                    return HraDataRes.getData();
                } else {
                    mView.showErrorUi(HraDataRes.getMsg());
                }
                return null;
            }
        }).subscribe(new Observer<HraDataRes.DataBean>() {

            @Override
            public void onSubscribe(@NonNull Disposable d) {
            }

            @Override
            public void onNext(@NonNull HraDataRes.DataBean dataBeen) {
                mData = dataBeen;
                if (dataBeen == null || dataBeen.getTotal() == null) return;
                getScore(dataBeen.getTotal());
            }

            @Override
            public void onError(@NonNull Throwable e) {
                LogUtil.e(e.toString(), TAG);
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public void getScore(String total) {
        if (!mFragment.isDataShow()) {
            if (mView == null) return;
            mView.showContentUI();//todo 判断是否已经显示
        }

        mView.showCircleProgressbar(Float.parseFloat(total));
//        mView.showCircleProgressbar(100);
//        mView.showCircleProgressbar_develop(Float.parseFloat(total));
    }
}
